package com.sky.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
public class SetmealController {
    @Resource
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation("新增套餐")
    public Result<String> add(@RequestBody Setmeal setmeal) {
        setmealService.add(setmeal);

        return Result.success("新增套餐成功");
    }

    @PutMapping
    @ApiOperation("修改套餐")
    public Result<String> edit(@RequestBody Setmeal setmeal) {
        setmealService.edit(setmeal);

        return Result.success("修改套餐成功");
    }

    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result<String> del(@RequestParam List<Long> ids) {
        try {
            setmealService.del(ids);

            return Result.success("批量删除套餐成功");
        } catch (DeletionNotAllowedException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取套餐")
    public Result<Setmeal> get(@PathVariable Integer id) {
        return Result.success(setmealService.getById(id));
    }

    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<Page<Setmeal>> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        return Result.success(setmealService.page(setmealPageQueryDTO));
    }
}
